﻿using DefaultScope;
using Fabrikam.Client.DataModel;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Fabrikam.Client.ViewModels
{
    public class SettingFlyoutItemViewModel : ViewModelBase
    {
        internal TaskCompletionSource<Boolean> tcs;

        private System.Guid serviceTicketID;

        private string title;

        private string description;

        private int statusValue;

        private int escalationLevel;

        private System.Nullable<System.DateTime> opened;

        private System.Nullable<System.DateTime> closed;

        private System.Nullable<int> customerID;

        public bool IsNew { get; set; }

        public System.Guid ServiceTicketID
        {
            get
            {
                return serviceTicketID;
            }
            set
            {
                serviceTicketID = value;
                base.RaisePropertyChanged(() => ServiceTicketID);
            }
        }

        public string Title
        {
            get
            {
                return title;
            }
            set
            {
                title = value;
                base.RaisePropertyChanged(() => Title);
            }
        }

        public string Description
        {
            get
            {
                return description;
            }
            set
            {
                description = value;
                base.RaisePropertyChanged(() => Description);
            }
        }

        public int StatusValue
        {
            get
            {
                return statusValue;
            }
            set
            {
                statusValue = value;

                switch (statusValue)
                {
                    case 1:
                    case 2:
                        if (!this.Closed.HasValue)
                            this.Closed = DateTime.Now;
                        break;
                    default:
                        this.Closed = null;
                        break;
                }
                base.RaisePropertyChanged(() => StatusValue);
            }
        }

        public int EscalationLevel
        {
            get
            {
                return escalationLevel;
            }
            set
            {
                escalationLevel = value;
                base.RaisePropertyChanged(() => EscalationLevel);
            }
        }

        public System.Nullable<System.DateTime> Opened
        {
            get
            {
                return opened;
            }
            set
            {
                opened = value;
                base.RaisePropertyChanged(() => Opened);
            }
        }

        public System.Nullable<System.DateTime> Closed
        {
            get
            {
                return closed;
            }
            set
            {
                closed = value;
                base.RaisePropertyChanged(() => Closed);
            }
        }

        public System.Nullable<int> CustomerID
        {
            get
            {
                return customerID;
            }
            set
            {
                customerID = value;
                base.RaisePropertyChanged(() => CustomerID);
            }
        }

        private List<Customers> customers;
        public List<Customers> Customers
        {
            get
            {
                return customers;
            }
            set
            {
                if (value == customers)
                    return;

                customers = value;

                this.RaisePropertyChanged(() => Customers);
            }
        }


        private Int32[] availableStates = new Int32[3] { 1, 2, 3 };

        public Int32[] AvailableStates
        {
            get
            {
                return availableStates;

            }

        }


        private Customers currentCustomer;
        public Customers CurrentCustomer
        {
            get
            {
                return currentCustomer;
            }
            set
            {
                if (value == currentCustomer)
                    return;

                currentCustomer = value;

                if (currentCustomer != null)
                    this.CustomerID = currentCustomer.ID;

                this.RaisePropertyChanged(() => CurrentCustomer);
            }
        }
        private async void GetCustomers()
        {
            this.Customers = await DataService.Current.GetCustomers();

            if (!this.CustomerID.HasValue)
                return;

            this.CurrentCustomer = this.Customers.FirstOrDefault((c) => c.ID == this.CustomerID);
        }
        private RelayCommand saveCommand;
        public RelayCommand SaveCommand
        {
            get
            {
                if (saveCommand != null) return saveCommand;

                saveCommand = new RelayCommand(async () =>
                {
                    var lst = new List<ServiceTickets>();

                    lst.Add(new ServiceTickets()
                        {
                            ServiceTicketID = this.ServiceTicketID,
                            Title = this.Title,
                            StatusValue = this.StatusValue,
                            Closed = this.Closed,
                            Description = this.Description,
                            CustomerID = this.CustomerID,
                            EscalationLevel = this.EscalationLevel,
                            Opened = this.Opened
                        });

                    if (!this.IsNew)
                        await DataService.Current.UpdateServiceTickets(lst);
                    else
                        await DataService.Current.InsertServiceTickets(lst);

                    if (this.tcs.Task.Status != TaskStatus.RanToCompletion)
                        this.tcs.SetResult(true);
                });

                return saveCommand;
            }
        }

        private RelayCommand cancelCommand;
        public RelayCommand CancelCommand
        {
            get
            {
                if (cancelCommand != null) return cancelCommand;

                cancelCommand = new RelayCommand(() =>
                                        {
                                            if (this.tcs.Task.Status != TaskStatus.RanToCompletion)
                                                this.tcs.SetResult(false);
                                        });

                return cancelCommand;
            }
        }
        internal void InitServiceTicket(ServiceTickets ticket)
        {
            if (ticket == null)
            {
                this.IsNew = true;
                ServiceTicketID = Guid.NewGuid();
                Description = String.Empty;
                StatusValue = 1;
                EscalationLevel = 0;
                Title = String.Empty;
                Opened = DateTime.Now;
                Closed = DateTime.Now;
                CustomerID = null;
            }
            else
            {
                // Make a copy to be able to edit this copy and not the base object (in cas, we cancel the action)
                ServiceTicketID = ticket.ServiceTicketID;
                Description = ticket.Description;
                StatusValue = ticket.StatusValue;
                EscalationLevel = ticket.EscalationLevel;
                Title = ticket.Title;
                Opened = ticket.Opened;
                Closed = ticket.Closed;
                CustomerID = ticket.CustomerID;
                this.IsNew = false;

            }
            this.tcs = new TaskCompletionSource<Boolean>();

            this.GetCustomers();

        }
        public SettingFlyoutItemViewModel()
        {
            this.IsNew = true;

            if (IsInDesignMode)
            {
                ServiceTicketID = Guid.NewGuid();
                Description = "Lorem Sempsum Description bla bla bla ...";
                StatusValue = 1;
                EscalationLevel = 1;
                Title = "Design Time";
                Opened = DateTime.Now.AddMonths(-5);
                Closed = DateTime.Now;
                CustomerID = 1;
            }

        }
    }
}
